package org.example.thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/*
 *Author:ZouHeng
 *Des: 测试创建线程的三种方法
 *Date:2021-01-04  10:34
 */
public class CreateThread {
    public static void main(String[] args) {
        //继承Thread类
        MyThread myThread = new MyThread("线程1");
        myThread.start();

        //实现Runnable接口
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable, "线程2");
        thread.start();
        //简化1
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " is running ");
            }
        }, "线程2-1").start();
        //简化2，Lambda表达式
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " is running ");

        }, "线程2-2").start();


        //实现Callable接口
        MyCallable myCallable = new MyCallable();
        FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
        new Thread(futureTask, "线程3").start();
        try {
            System.out.println("子线程的返回值: " + futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
